--
-- Author: mm
-- Date: 2019-06-05 15:09:34
--
-- SuperrankView
--

local fileName = string.getModeName(...)
local SuperRankView = class(fileName, app.mvc.ViewBase)
local SecondLevelListMenu = require("app.widget.listmenu.SecondLevelListMenu")
local SuperRankHelper = require("app.superRank.SuperRankHelper")

local RankNormalContentPanel = require("app.widget.rank.panels.RankNormalContentPanel")
local RankNormalBottomPanel = require("app.widget.rank.panels.RankNormalBottomPanel")
local SuperRankAvatarPanel = import(".panels.SuperRankAvatarPanel")
local RankDataConvertHelper = require("app.widget.rank.RankDataConvertHelper")

SuperRankView.VIEW_ROOT_RES = Res.SuperRankView

function SuperRankView:init()
    self.super.init(self)
    self._ui["Panel/Button_back"]:addClickEventListener(function()
		executeFunc(self._onCloseView)
    end)
    self._menuList = SecondLevelListMenu.new(self._ui["Panel/ScrollView"], {
		focusChangeCallback = handler(self, self._onSwitchPanel),
	})
    self:registerViewEvent({
        {name = ViewEvent.SuperRank_ReloadMenu, callback = handler(self, self._onReloadMenu)},
        {name = ViewEvent.SuperRank_RefreshContent, callback = handler(self, self._onRefreshContent)},
    })

    self._menuData = SuperRankHelper.getBaseMenu()
    self._bottomPanel = RankNormalBottomPanel.create(self._ui["Panel/Panel_r/Panel_Bottom"])
    self._avatarPanel = SuperRankAvatarPanel.create(self._ui["Panel/Panel_r/Panel_Avatar"])
    self._contentPanel = RankNormalContentPanel.create(self._ui["Panel/Panel_r/Panel_Conent"])

    self:_onRefreshContent("", SuperRankHelper.getEmptyData(Enums.SuperRankMenu.CSArena))

    self._bottomPanel:onOperate(handler(self, self._onBottomOperate))
    self._contentPanel:onOperate(handler(self, self._onContentOperate))

    self._ui["Panel/Panel_tlt/tlt"]:setString(L("SuperRankName"))
    self._curPage = nil
end

-- 回包 数组
function SuperRankView:_onReloadMenu(eventName, datas, key)
    if not datas then
        return
    end
    if #datas == 0 then
        self:_onRefreshContent("", SuperRankHelper.getEmptyData(key))
    else
        for k ,v in pairs(self._menuData) do
            if v.key == key then
                v.children = datas
                break
            end
        end
        self:_reloadMenuData(self._menuData)
    end

end

-- 切换 右边页签
function SuperRankView:_onSwitchPanel( data )
    -- body
    if not data then
		return
    end

    self._curSelectData = data
    if data.key == Enums.SuperRankMenu.CSArena then
        if not data.isReq then
            -- 这里请求  列表子项
            UD.superRankData:C2S_GetCrossEliteHistoryInfo(Enums.TeamTarget.eliteMatchArena)
            data.isReq = true
        else
            self:_onRefreshContent("", SuperRankHelper.getEmptyData(data.key))
        end
    elseif data.key == Enums.SuperRankMenu.CSDuel then
        if not data.isReq then
            -- 这里请求  列表子项
            UD.superRankData:C2S_GetCrossEliteHistoryInfo(Enums.TeamTarget.eliteMatchDuel)
            data.isReq = true
        else
            self:_onRefreshContent("", SuperRankHelper.getEmptyData(data.key))
        end
    elseif data.key == Enums.SuperRankMenu.CSGuildWar then
        if not data.isReq then
            -- 这里请求  列表子项
            UD.superRankData:C2S_GetCrossGuildHistoryInfo()
            data.isReq = true
        else
            self:_onRefreshContent("", SuperRankHelper.getEmptyData(data.key))
        end
    else
        -- 根据 data 数据刷新 其他
        if data.tp == Enums.SuperRankMenu.CSArena then
            UD.superRankData:C2S_GetCrossEliteHistoryRank(Enums.TeamTarget.eliteMatchArena, data.season)
        elseif data.tp == Enums.SuperRankMenu.CSDuel then
            UD.superRankData:C2S_GetCrossEliteHistoryRank(Enums.TeamTarget.eliteMatchDuel, data.season)
        elseif data.tp == Enums.SuperRankMenu.CSGuildWar then
            UD.superRankData:C2S_GetCrossGuildHistoryRank(data.season)
        end
    end
end

function SuperRankView:_getFirstSelectByKey(key)
    for k , v in pairs(self._menuData) do
        if v.key == key then
            return k
        end
    end
    return 1
end

function SuperRankView:openByKey(key)
    local index = self:_getFirstSelectByKey(key)
    self:_reloadMenuData(self._menuData, index)
end



function SuperRankView:_onRefreshContent(eventName, data)
    if not data then
        return
    end
    self._contentsData = data
    self._contentPanel:cleanTag()
    self._bottomPanel:setData(data.bottomInfo)
    self._curPage = nil
    self:_refreshPanel(1)
end


function SuperRankView:_onBottomOperate(eventName, params)
    if eventName == "page" then
        self:_refreshPanel(params)
    elseif eventName == "findMyPos" then
        if params and self._contentsData then
            local rank = params.rank
            local avatarPageNum = self._contentsData.avatarPageNum or 0
            local pageNum = self._contentsData.pageNum or 0
            local avatarIndex = avatarPageNum * 3
            local page = nil
            local startIndex = nil
            if rank <= avatarIndex then
                page = math.ceil( rank/ 3)
            else
                rank = rank - avatarIndex
                startIndex = rank
                page = avatarPageNum + math.ceil( rank/ RankDataConvertHelper.PageCellNum)
            end
            if page and page > 0 and page <= pageNum then
                self:_refreshPanel(page, startIndex)
            end
        end
    end
end

function SuperRankView:_onContentOperate(eventName, params)
    if eventName == "lookPlayer" then
        UIHandler:showHeadMenu({online=true, attr=params.userBaseInfo, pos = params.pos})
    elseif eventName == "lookGuild" then
        app:sendMsg("SuperRankController", "popGuildDetailView", params)
    elseif eventName == "scrollToPage" then
        self._bottomPanel:setPage(params)
    end
end

function SuperRankView:_refreshPanel(page, startIndex)
    if not page then
        page = 1
    end
    if not self._contentsData then
        return
    end
    local isSamePage = false
    if self._curPage == page then
        isSamePage = true
    end
    self._curPage = page
    local data = self._contentsData
    -- avatarr 没有 或者  页码大于
    if data.pageNum == 0 or data.avatarPageNum == 0 or page > data.avatarPageNum then
        -- content
        self._avatarPanel:setVisible(false)
        self._contentPanel:setVisible(true)
        self._contentPanel:jumpToPage(data.rankData, page, startIndex)
    else
        self._avatarPanel:setVisible(true)
        self._contentPanel:setVisible(false)
        if not isSamePage then
            local index = (page - 1) * 3
            local avatarPageData = {page = page, avatar1 = data.avatars[index + 1], 
                    avatar2 = data.avatars[index + 2], avatar3 = data.avatars[index + 3], title = data.title}
            self._avatarPanel:setData(avatarPageData)
        end
    end
    self._bottomPanel:setPage(page)
end

function SuperRankView:_reloadMenuData(d, firstSelect, secondSelect)
    self._menuData = d 
    self._menuList:reloadMenuData(self._menuData, {
		firstSelect = firstSelect,
		secondSelect = secondSelect,
	})
end

return SuperRankView
